//
//  UIViewController+YCExtension.m
//  YCPublicDemo
//
//  Created by zhangji on 2017/5/26.
//  Copyright © 2017年 xinhuanwangluo. All rights reserved.
//

#import "UIViewController+YCExtension.h"

@implementation UIViewController (YCExtension)

- (UIViewController *)yc_previousViewController
{
    UINavigationController * nav = self.navigationController;
    if ( nav )
    {
        NSArray * controllers = nav.viewControllers;
        if ( 0 == controllers.count )
            return nil;
        
        NSInteger index = [controllers indexOfObject:self];
        if ( NSNotFound == index )
            return nil;
        
        if ( index > 0 )
        {
            return [controllers objectAtIndex:(index - 1)];
        }
    }
    
    return nil;
}

- (UIViewController *)yc_nextViewController
{
    UINavigationController * nav = self.navigationController;
    if ( nav )
    {
        NSArray *	controllers = nav.viewControllers;
        NSInteger	index = [controllers indexOfObject:self];
        
        if ( controllers.count > 1 && (index + 1) < controllers.count )
        {
            return [controllers objectAtIndex:(index + 1)];
        }
    }
    
    return nil;
}


+ (UIViewController *)yc_getCurrentVc
{
    //获取当前屏幕显示的viewcontroller
    
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}


+ (UIViewController *)yc_getCurrentRootViewController
{
    
    UIViewController *result;
    
    // Try to find the root view controller programmically
    
    // Find the top window (that is not an alert view or other window)
    
    UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow];
    
    if (topWindow.windowLevel != UIWindowLevelNormal)
        
    {
        
        NSArray *windows = [[UIApplication sharedApplication] windows];
        
        for(topWindow in windows)
            
        {
            
            if (topWindow.windowLevel == UIWindowLevelNormal)
                
                break;
            
        }
        
    }
    
    UIView *rootView = [[topWindow subviews] objectAtIndex:0];
    
    id nextResponder = [rootView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        
        result = nextResponder;
    
    else if ([topWindow respondsToSelector:@selector(rootViewController)] && topWindow.rootViewController !=nil)
        
        result = topWindow.rootViewController;
    
    else
        
        NSAssert(NO,@"ShareKit: Could not find a root view controller. You can assign one manually by calling [[SHK currentHelper] setRootViewController:YOURROOTVIEWCONTROLLER].");
    
    return result;
    
}

- (void)cleanPresentedVCWithCompletion:(void (^)(void))completion {
    [self cleanPresentedVC:self completion:completion];
}

- (void)cleanPresentedVC:(UIViewController *)presentedVC completion:(void (^)(void))completion {
    
    if ([presentedVC presentedViewController]) {
        [presentedVC cleanPresentedVC:[presentedVC presentedViewController] completion:completion];
    } else {
        UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
        if ([rootVC presentedViewController]) {
            [presentedVC dismissViewControllerAnimated:NO completion:^{
                [rootVC cleanPresentedVC:rootVC completion:completion];
            }];
        } else {
            if (completion) {
                completion();
            }
        }
    }
}

@end
